package com.ymatou.iapi.userservice.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.CloseAccountBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.CloseAccountCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.UserinfoUpdaterecord;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
/**
*用户注销
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CloseAccount{
	private static CloseAccountBean closeaccountBean;
	private static CloseAccountCall closeaccountCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户注销");
	}

	@Before
	public void caseUp() {
		closeaccountBean=new CloseAccountBean();
		closeaccountCall=new CloseAccountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(closeaccountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(CloseAccountBean closeaccountBean2, CreateUserReq createUserBean2) throws Exception {
		JUserWapper ju=new JUserWapper();
		Logger.comment("验证Userinfo");
		List<Userinfo> lui = ju.selectUserinfoByUid(closeaccountBean2.getUserId());
		Logger.verifyEquals(1, lui.size(), "验证记录条数");
		Userinfo ui = lui.get(0);
		if(createUserBean2.getFastRegModel()!=null&&createUserBean2.getFastRegModel()&&createUserBean2.getLoginId()==null){
			Logger.verifyNotNull(ui.getLoginId(), "验证LoginId");
		}else{
			Logger.verifyEquals(createUserBean2.getLoginId(), ui.getLoginId(), "验证LoginId");
		}
		Logger.verifyEquals(createUserBean2.getMobile()+"."+closeaccountBean2.getUserId()+".delete", ui.getLoginMobile(), "验证LoginMobile");
		Logger.verifyEquals(createUserBean2.getMail()+"."+closeaccountBean2.getUserId()+".delete", ui.getLoginMail(), "验证LoginMail");
		Logger.verifyEquals(createUserBean2.getUserType().getCode(), ui.getUserType(), "验证UserType");
		//2是删除
		Logger.verifyEquals(2, ui.getUserState(), "验证UserState");
		Logger.verifyEquals(1, ui.getUserLevel(), "验证Userlevel");
		
		Logger.comment("验证Userinfoex");
		List<Userinfoex> luiex = ju.selectUserinfoexByUid(closeaccountBean2.getUserId());
		Logger.verifyEquals(1, luiex.size(), "验证记录条数");
		Userinfoex uiex = luiex.get(0);
		Logger.verifyEquals(createUserBean2.getChannelSource().getCode(), uiex.getChannelSource(), "验证ChannelSource");
		Logger.verifyEquals(createUserBean2.getRegSource().getCode(), uiex.getRegSource(), "验证RegSource");
		if(createUserBean2.getTerminalSource()!=null){
			Logger.verifyEquals(createUserBean2.getTerminalSource().getCode(), uiex.getTerminalSource(), "验证TerminalSource");
		}
		if(createUserBean2.getVisitLanguage()!=null){
			Logger.verifyEquals(createUserBean2.getVisitLanguage().getCode(), uiex.getVisitLanguage(), "验证getVisitLanguage");
		}
		Logger.verifyEquals(createUserBean2.getEquimentid(), uiex.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(createUserBean2.getDeviceId(), uiex.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(createUserBean2.getClientIp(), uiex.getClientIp(), "验证ClientIp");
		//添加日志记录 select * from userinfo_updaterecord where OldBizValue = '113456754657' and UserBizType = 99
		List<UserinfoUpdaterecord> logs = ju.selectUserinfoUpdaterecord(closeaccountBean2.getUserId());
		List<UserinfoUpdaterecord> dlog = logs.stream().filter(log->{return log.getUserBizType()==99;}).collect(Collectors.toList());
		Logger.debug(FormatUtil.beanToGSONString(dlog));
		Logger.verifyEquals(1, dlog.size(), "验证有一条删除记录");
		Logger.verifyEquals(createUserBean2.getMobile(), dlog.get(0).getOldBizValue(), "验证OldBizValue");
		Logger.verifyEquals(99, dlog.get(0).getUserBizType(), "验证UserBizType");
		Logger.verifyEquals(closeaccountBean2.getReason(),dlog.get(0).getNewBizValue(), "验证newBizValue");
		String token = new UserTokenWapper().getAccessTokenByUid(closeaccountBean2.getUserId());
		Logger.verifyIsNull(token, "验证token已删除");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseAccount_001() {
		Logger.start(true,"用户注销,成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			closeaccountBean.setUserId(uid);
			closeaccountBean.setReason("test");
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("true", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(closeaccountBean, cur);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseAccount_002() {
		Logger.start(true,"不传参数uid失败");
		try {
			closeaccountBean.setReason("test");
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("false", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseAccount_003() {
		Logger.start(true,"用户注销多次");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			closeaccountBean.setUserId(uid);
			closeaccountBean.setReason("test");
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("true", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(closeaccountBean, cur);
			//重复注销 幂等
			closeaccountCall.callService();			
			Logger.verifyEquals("true", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseAccount_004() {
		Logger.start(true,"不传参数uid失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			closeaccountBean.setUserId(uid);
//			closeaccountBean.setReason("test");
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("false", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseAccount_005() {
		Logger.start(true,"用户注销,safetyCheck为true检测");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			JUserCallService.UpdateLoginPwd(uid, "abcdef123");
			closeaccountBean.setUserId(uid);
			closeaccountBean.setReason("test");
			closeaccountBean.setSafetyCheck(true);
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("false", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("198", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseAccount_006() {
		Logger.start(true,"用户注销,safetyCheck为false不检查");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			JUserCallService.UpdateLoginPwd(uid, "abcdef123");
			closeaccountBean.setUserId(uid);
			closeaccountBean.setReason("test");
			closeaccountBean.setSafetyCheck(false);
			closeaccountCall.setData(closeaccountBean);
			closeaccountCall.callService();			
			Logger.verifyEquals("true", closeaccountCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", closeaccountCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(closeaccountBean, cur);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}